package com.yvon.maple.service.msg.sub.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yvon.boot.mybatis.BaseServiceImpl;
import com.yvon.maple.cloud.web.filter.LoginUserUtil;
import com.yvon.maple.constants.SystemConstant;
import com.yvon.maple.service.msg.sub.mapper.SubscribeActiveMapper;
import com.yvon.maple.service.msg.sub.model.entity.SubscribeActive;
import com.yvon.maple.service.msg.sub.service.SubscribeActiveService;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.yvon.maple.constants.SystemConstant.Common.LIMIT_ONE;

/**
 * @author : Yvon
 * @since : 2021-12-30
 */
@Service
public class SubscribeActiveServiceImpl extends BaseServiceImpl<SubscribeActive, SubscribeActiveMapper> implements SubscribeActiveService {

    @Override
    public Boolean updateList(List<SubscribeActive> activeList) {
        if (null == activeList) {
            return false;
        }
        if (CollUtil.isNotEmpty(activeList)) {
            QueryWrapper<SubscribeActive> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", LoginUserUtil.getUserId());
            remove(queryWrapper);
            activeList.forEach(a -> a.setUserId(LoginUserUtil.getUserId()));
            return saveBatch(activeList);
        }
        return false;
    }

    @Override
    public SubscribeActive getBySubscribeId(Long subscribeId) {
        return lambdaQuery()
                .eq(SubscribeActive::getSubscribeId, subscribeId)
                .last(LIMIT_ONE)
                .one();
    }


}
